"""
Case Type   : cm_ctl
Case Name   : 参数event_triggers值不符合json,合理报错
Create At   : 2023/04/24
Owner       : opentestcase012
Description :
    1.修改所有节点的参数值event_triggers;
Expect      :
    1.参数值修改失败，The value of event_triggers is not a json
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.pri_user = Node('PrimaryDbUser')

    def test_cm(self):
        text = '----step1:修改所有节点的参数值event_triggers不符合json; expect:修改失败----'
        self.log.info(text)
        value_list = ['{{', '{{""}}', '{{1:2}}']
        for value in value_list:
            cmd = f"source {macro.DB_ENV_PATH};" \
                f"cm_ctl set --param --agent -k event_triggers='{value}';"
            self.log.info(cmd)
            res = self.common.get_sh_result(self.pri_user, cmd)
            self.assertIn("The value of event_triggers is not a json", res,
                          "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '----step2:恢复参数值并执行reload生效; expect:成功----'
        self.log.info(text1)
        cmd = f"source {macro.DB_ENV_PATH};" \
            "cm_ctl set --param --agent -k event_triggers='{{}}';" \
            f"cm_ctl reload --param --agent"
        self.log.info(cmd)
        cm_ctl_res1 = self.common.get_sh_result(self.pri_user, cmd)
        self.assertEqual(2, cm_ctl_res1.count('success'),
                         "执行失败:" + cm_ctl_res1)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
